package com.bearingnavi.android.overlays;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Typeface;

import com.bearingnavi.android.util.ClickableOverlay;
import com.bearingnavi.android.util.MarshalOverlay;
import com.bearingnavi.android.util.OverlayUtil;
import com.bearingnavi.android.views.RotateView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class WayPointOverlay extends Overlay implements MarshalOverlay, ClickableOverlay {

	private GeoPoint geoPoint;
	private int index;
	private String lineCaption;
	private int POINT_COLOR = Color.CYAN;
	
	public WayPointOverlay() {
		super();
	}
	
	public WayPointOverlay(GeoPoint geoPoint, int index) {

		this.geoPoint = geoPoint;
		this.index = index;
		this.lineCaption = "#"+(index);
	}
	
	public int getIndex() {
		return index;
	}
	
	public GeoPoint getGeoPoint() {
		return this.geoPoint;
	}
		
	public void draw(Canvas canvas, MapView mapv, boolean shadow) {
		super.draw(canvas, mapv, shadow);
		
		Paint paint = new Paint();
		paint.setDither(true);
		paint.setColor(POINT_COLOR);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStrokeWidth(10);

		Point point = new Point();

		Projection projection = mapv.getProjection();
		projection.toPixels(geoPoint, point);
		
		
		Paint textPaint = new Paint();
	    textPaint.setColor(Color.WHITE);
	    textPaint.setTextAlign(Paint.Align.CENTER);
	    textPaint.setTextSize(26);
	    textPaint.setTypeface(Typeface.DEFAULT_BOLD);
	    textPaint.setShadowLayer(2, 0, 2, Color.GRAY);
	    
	    Paint bgPaint = new Paint();
	    bgPaint.setColor(Color.BLACK);
	    bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
	    bgPaint.setAlpha(75);
	    
		float textWidth= textPaint.measureText(lineCaption);
		
		canvas.drawPoint(point.x, point.y, paint);
		
		canvas.save();
		
		canvas.rotate(RotateView.getInstance().getBearing(), point.x, point.y-20);
		canvas.drawRect(point.x-10-(textWidth/2), point.y-10, point.x+10+(textWidth/2), point.y-50, bgPaint);
		canvas.drawText(lineCaption, point.x, point.y-20, textPaint);
		
		canvas.restore();
		
	}

	public String marshal() {
		return new StringBuilder()
		.append(geoPoint.getLatitudeE6())
		.append(MarshalOverlay.FIELDS_SEPARATOR)
		.append(geoPoint.getLongitudeE6())
		.append(MarshalOverlay.FIELDS_SEPARATOR)
		.append(index).toString();
	}

	public void unmarshal(String[] objectFields, boolean createAsSnapshot, MapView mapView) {
		GeoPoint geoPoint = new GeoPoint(
				Integer.parseInt(objectFields[1]),
				Integer.parseInt(objectFields[2]));
		
		this.geoPoint = geoPoint;
		if (createAsSnapshot) {
			this.index = Integer.parseInt(objectFields[3]);
		} else {
			this.index = OverlayUtil.getNextWayPointOverlayIndex(mapView);
		}
		this.lineCaption = "#"+(index);
	}

	public GeoPoint getAnimateTo() {
		return geoPoint;
	}

	public GeoPoint getClickablePoint() {
		return geoPoint;
	}

	public void onChosenEvent(MapView mapView) {
		this.lineCaption = "#"+(index)+" You clicked on me";
		POINT_COLOR = Color.RED;
	}

	public void onReleaseChosenEvent(MapView mapView) {
		this.lineCaption = "#"+(index);
		POINT_COLOR = Color.CYAN;
	}
	
	
}
